package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rules.admin.domain.Page;
import com.rules.admin.entity.*;
import com.rules.admin.login.UserHolder;
import com.rules.admin.publish.RedisNotifer;
import com.rules.admin.service.*;
import com.rules.admin.service.decision.DecisionTableService;
import com.rules.admin.utils.Constant;
import com.rules.admin.utils.ResultUtils;
import com.rules.admin.vo.RelpaceRuleMenuVO;
import com.rules.admin.vo.RuleMenuVO;
import com.rules.admin.vo.RuleUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

@RequestMapping({"/rulePackage"})
@Controller
public class RulePackageController {
   @Autowired
   private RulePackageService rulePackageService;
   @Autowired
   private RuleAssemblyService ruleAssemblyService;
   @Autowired
   private RuleVariableService ruleVariableService;
   @Autowired
   private UnitCaseService unitCaseService;
   @Autowired
   private RedisNotifer redisNotifer;
   @Autowired
   private ReportService reportService;
   @Autowired
   private DecisionTableService decisionTableService;
   @Autowired
   private UserHolder<RuleUser> userHolder;
   @Autowired
   private RuleUserRoleService ruleUserRoleService;
   @Autowired
   private ProjectService projectService;
   @Autowired
   private BusinessModelService businessModelService;
   @Autowired
   private BusinessModelAttributeService businessModelAttributeService;


   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Page queryPage(String name, String artifactId, String remark, Page page) {
      return this.rulePackageService.queryPage(name, artifactId, remark, page);
   }

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addRulePackage(@RequestBody RulePackage rulePackage) {
      this.rulePackageService.insert(rulePackage);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public RulePackage getRulePackage(@PathVariable("id") Long id) {
      return this.rulePackageService.getById(id);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateRulePackage(@RequestBody RulePackage rulePackage) {
      this.rulePackageService.update(rulePackage);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteRulePackage(@PathVariable("id") Long id) {
      if (this.ruleAssemblyService.getRuleAssemblyByPackageId(id).size() == 0 && this.decisionTableService.getDecisionTablesByPackageId(id).size() == 0) {
         if (this.ruleVariableService.getByScopeAndScopeId(1, id).size() == 0) {
            this.rulePackageService.delete(id);
            return ResultUtils.getSuccessResultData();
         } else {
            return ResultUtils.getFaildResultData(new String[]{"该规则包下有变量，请先删除！"});
         }
      } else {
         return ResultUtils.getFaildResultData(new String[]{"该规则包下有规则集或决策表，请先删除！"});
      }
   }

   @RequestMapping(
      value = {"/unitCase"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<UnitCase> queryUnitCasePage(String groupId, String artifactId, IPage<UnitCase> page) {
      return this.unitCaseService.getUnitCaseByGroupIdandArtifactId(groupId,artifactId,page);
   }

   @RequestMapping(
      value = {"/getVersions"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getRulePackageVersions(IPage page, String groupId, String artifactId) {
      return ResultUtils.getSuccessResultData(this.rulePackageService.queryPagePackages(groupId, artifactId, page));
   }

   @RequestMapping(
      value = {"/publish/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> publish(@PathVariable("id") Long id) {
      RulePackage rulePackage = this.rulePackageService.getById(id);
      this.redisNotifer.notify(rulePackage);
      this.rulePackageService.updateRulePackageByPublished(id);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/getRulePackages/{projectId}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getRulePackages(@PathVariable("projectId") Long projectId) throws InvocationTargetException, IllegalAccessException {
      RuleUser ruleUser = (RuleUser)this.userHolder.getUser();
      if (ruleUser.equals(RuleUser.ANONYMOUS_USER)) {
         return ResultUtils.getSuccessResultData("reload");
      } else if (this.ruleUserRoleService.getByUserId(ruleUser.getId()).getRoleId() == Constant.PERMISSION_ADMIN) {
         List<RulePackage> packages = this.rulePackageService.getAll(projectId);
         return ResultUtils.getSuccessResultData(packages);
      } else {
         return ResultUtils.getSuccessResultData(this.rulePackageService.getPermissionPacakge(projectId));
      }
   }

   @RequestMapping(
      value = {"/getRulePackageMenu/{packageId}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getRulePackageMenu(@PathVariable("packageId") long packageId) {
      List<Map<String, Object>> data = this.rulePackageService.getRulePackMenu(packageId);
      return ResultUtils.getSuccessResultData(data);
   }

   @RequestMapping(
      value = {"/updateRuleAssemblyPriority"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> updateRuleAssemblyPriority(@RequestBody RuleMenuVO[] ruleMenuVOs) {
      this.rulePackageService.updateRuleAssemblyPriority(ruleMenuVOs);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/updateRulePriority"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> updateRulePriority(@RequestBody RuleMenuVO[] ruleMenuVOs) {
      this.rulePackageService.updateRulePriority(ruleMenuVOs);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/replaceRuleAssemblyPriority"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> replaceRuleAssemblyPriority(@RequestBody RelpaceRuleMenuVO relpaceRuleMenuVO) {
      this.rulePackageService.updateRuleAssemblyPriority(relpaceRuleMenuVO.getOrig(), relpaceRuleMenuVO.getDest());
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/replaceRulePriority"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> replaceRulePriority(@RequestBody RelpaceRuleMenuVO relpaceRuleMenuVO) {
      this.rulePackageService.updateRulePriority(relpaceRuleMenuVO.getOrig(), relpaceRuleMenuVO.getDest());
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/getPackage"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getPackage() {
      return ResultUtils.getSuccessResultData(this.rulePackageService.getPackage());
   }

   @RequestMapping(
      value = {"/getPagePackage"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<RulePackage> getPagePackage(IPage<RulePackage> page, String name) {
      return this.rulePackageService.getPagePackage(page, name);
   }

   @RequestMapping(
      value = {"/returnId"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> ReturnRulePackageId(@RequestBody RulePackage rulePackage) {
      return ResultUtils.getSuccessResultData(this.rulePackageService.insert(rulePackage));
   }

   @RequestMapping(
      value = {"/{packageId}/businessModule"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getBusinessModelsId(@PathVariable("packageId") @RequestBody Long packageId) {
      RulePackage rulePackage = this.rulePackageService.getById(packageId);
      return ResultUtils.getSuccessResultData(rulePackage.getBusinessModelId() + "");
   }

   @RequestMapping(
      value = {"/getBusinessModelAttributes"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getBusinessModelAttributes(Long id) {
      BusinessModel businessModel = this.businessModelService.getById(this.rulePackageService.getById(id).getBusinessModelId());
      if (businessModel == null) {
         return ResultUtils.getSuccessResultData();
      } else {
         List<BusinessModelAttributeEntity> businessModelAttributeEntities = this.businessModelAttributeService.getModelAttributesByModelId(businessModel.getId());
         return ResultUtils.getSuccessResultData(businessModelAttributeEntities);
      }
   }

   @RequestMapping(
      value = {"/updateRulePackage"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateRulePackageInstall(@RequestBody RulePackage rulePackage) {
      this.rulePackageService.updateRulePackageInstallVO(rulePackage);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/getPackageNames"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getPackageNames(Long groupId) {
      return ResultUtils.getSuccessResultData(this.rulePackageService.getPackageNames(groupId));
   }

   @RequestMapping(
      value = {"/copy"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> copyRulePackage(@RequestBody RulePackage rulePackage) {
      BusinessModel businessModel = new BusinessModel();
      this.businessModelService.save(businessModel);
      this.rulePackageService.copy(rulePackage, businessModel.getId());
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/getCode"},
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public List<Map<String, String>> getMesByName(@RequestBody RulePackage rPackage) {
      List<RulePackage> list = this.rulePackageService.findByName(rPackage.getName());
      List<Map<String, String>> result = new ArrayList();
      Iterator var4 = list.iterator();

      while(var4.hasNext()) {
         RulePackage rulePackage = (RulePackage)var4.next();
         Map map = new HashMap();
         map.put("name", rulePackage.getName());
         map.put("code", rulePackage.getArtifactId());
         result.add(map);
      }

      return result;
   }

   @RequestMapping(
      value = {"/getProjectPath"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public String getProjectPath(String projectName, String projectId) {
      StringBuilder sb = new StringBuilder();
      sb.append(projectName);
      Project project = this.projectService.getById(Long.valueOf(projectId));
      boolean flag = true;

      while(true) {
         while(flag) {
            if (project.getParentId() != null && !"".equals(project.getParentId())) {
               project = this.projectService.getById(Long.valueOf(project.getParentId()));
               sb.insert(0, project.getProjectName() + "→");
            } else {
               flag = false;
            }
         }

         return sb.toString();
      }
   }
}
